package com.sssoft.Yundian.activity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.sssoft.Yundian.R;
import com.sssoft.Yundian.Dao.DataDao;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SigninActivity extends Activity {

	private EditText et_info;
	private EditText et_infoo;
	private DataDao dao;
	private Button button1;
	private Button button3;
	private Button button2;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_signin);
		dao = new DataDao(this);
		et_info = (EditText) findViewById(R.id.et_info);
		et_infoo = (EditText) findViewById(R.id.et_infoo);

		button1 = (Button) findViewById(R.id.button1);
		button2 = (Button) findViewById(R.id.bt_Menu);
		button3 = (Button) findViewById(R.id.button3);

		button2.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SigninActivity.this, LoginActivity.class);
				startActivity(intent);
				finish();
			}
		});

		button3.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(SigninActivity.this, LoginActivity.class);
				startActivity(intent);
				finish();
			}
		});

		button1.setOnClickListener(new Button.OnClickListener() {

			// 验证，将文本账号密码转换成字符串形式
			@Override
			public void onClick(View view) {
				String newname = et_info.getText().toString();
				String password = et_infoo.getText().toString();

				String pattern = "^[1][0-9]{10}$";

				// 创建 Pattern 对象
				Pattern r = Pattern.compile(pattern);

				// 现在创建 matcher 对象
				Matcher m = r.matcher(newname);
				if (m.find()) {
					if (dao.CheckIsDataAlreadyInDBorNot(newname)) {
						Toast.makeText(SigninActivity.this, "用户已存在", Toast.LENGTH_SHORT).show();

					} else {
						if (dao.register(newname, password)) {
							Toast.makeText(SigninActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
							Intent intent = new Intent(SigninActivity.this, LoginActivity.class);
							startActivity(intent);
							finish();
						}

					}
				} else {
					Toast.makeText(SigninActivity.this, "请输入正确的手机号!", Toast.LENGTH_SHORT).show();
				}
			}
		});

	}
}
